﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class UserControl_Register : System.Web.UI.UserControl
{
    TblCustomerDAO customerDAO = new TblCustomerDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool UserAccount_CheckExists()
    {
        bool bExists = false;
        try
        {
            bExists = customerDAO.CheckExits(txtEmail.Text, 1);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }

    protected void cmdSubmit_Click(object sender, ImageClickEventArgs e)
    {
        bool flag = false;
        lblMessage.Text = String.Empty;

        if (UserAccount_CheckExists())
        {
            lblMessage.Text = "Email is exist.";         
            return;
        }
        try
        {
            TblCustomers customer = new TblCustomers();
            customer.CusID = 0;
            customer.CusAddress = txtAdress.Text;
            customer.FName = txtFName.Text;
            customer.LName = txtLName.Text;
            customer.MobilePhone = txtphone.Text;
            customer.Password = Encryption.EncryptToString(txtPassword.Text);
            customer.Postcode = "";
            customer.Suburb = "";
            customer.CusState = "";
            customer.EmailAdress = txtEmail.Text;
            customer.HomePhone = "";
            customer.WorkPhone = "";
            customer.Company = "";
            customer.IsAdmin = false;
            customer.Notes = "";

            customerDAO.Insert_Up(customer, 1);

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
        }
        if (flag)
        {
            ScriptManager.RegisterStartupScript(cmdSubmit, cmdSubmit.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }
    }
}
